Skip to content

Translator: tests, expected WGSL and stricter errors - #142

Merged
narrowstacks merged 3 commits into
mainfrom
test-t2-translator
Dec 1, 2026
Merged

narrowstacks merged 3 commits into
mainfrom
test-t2-translator

Conversation

@narrowstacks

Copy link
Copy Markdown
Owner

Item T2 of the Rust review (#92). Until now the translator had no tests. It also accepted some Metal it couldn't translate faithfully, so the output could pass naga and still mean something else.

Tests (shaders/msl2wgsl/tests)

  • snippets.rs (46 tests): a small Metal snippet for each rule in render-wgpu/wgsl/README.md, compared against the WGSL lines it should produce, with each translation validated by naga. Covered: literal spellings and conversions, splats, C's usual arithmetic conversions, compound assignment converting back, truthiness, casts and constructors, shifts, precedence and the parentheses WGSL needs, % and fmod, the ternary as select and as a hoisted if, swizzle assignment, x++ inside an expression, float ++, copied parameters, switch case grouping, for/while/do-while/else-if, pow guards, round/rint, renamed builtins, mix/clamp/select splats, macros and M_PI_F, reserved words, struct defaults, constant tables, thread references and array pointers, resource parameters (one copy per global), renames, the three samplers plus coord::pixel and level(), texture read/write/size, and kernel, vertex and fragment entry points.
  • Negative tests check that unsupported Metal stops with test.metal:LINE and a clear message: matrices, templates, the comma operator, assignment inside an expression, switch fallthrough, several for steps, ++ in a loop condition or after &&, a vector component passed by reference, pointer arithmetic, a resource that isn't a plain name, isnan/isinf, vector ternary conditions, unknown names and functions, preprocessor and lexer errors, and an unclosed block.
  • golden.rs: translates every module from shaders/metal and compares it byte for byte with tests/golden/<module>.wgsl. It also validates each module with its bindings, as --check does. Rewrite the expected files with DEEPFIELD_BLESS=1 cargo test --test golden (run from shaders/msl2wgsl), the same variable the CLI golden test uses. The files are LF on every platform (.gitattributes).

Stricter errors and fixes

  • Samplers: properties are checked where the sampler is declared. Anything the three bound samplers can't reproduce is now an error instead of a silent fallback to linearClamp. That covers an unknown property, a missing filter::linear (Metal's default is nearest), address::repeat on both axes, and mip_filter::linear without the repeating sampler. Properties with parentheses (max_anisotropy(4)) now parse, so the error names them.
  • Texture methods: read(p, lod), get_width(lod) and write(v, p, lod) are errors; the translator used to drop the level. level() with no value is an error, where it used to panic.
  • Arity: builtins and struct constructors with the wrong argument count are errors. Extra arguments used to be dropped, and a missing one panicked (dot(a), mix(a, b), tex.read()).
  • round: Metal rounds halves away from zero, WGSL's round rounds them to even. It now goes through a metal_round_* helper, sign(x) * floor(abs(x) + 0.5). This affects Neutron Star Merger (neutronMerger.metal:134) and Black Hole Merger (blackHoleMerger.metal:343); those are the only diffs in the generated WGSL. rint still maps to WGSL round.
  • int_consts: a local const int N used to size later arrays in every function parsed after it. It is now scoped to its block.
  • Module specs: a kernel missing from MODULES.kernels is an error; it used to get a silent 8x8 with no bounds check. The three bloom kernels are now listed explicitly with the same 8x8 and no bounds check, so their output is unchanged. A rename that matches no resource parameter of an entry point is also an error.
  • Error locations: lexer and preprocessor errors read file.metal:N: like the rest. An unclosed /* is reported instead of ignored. The parser errors that had no location now have one, and a using line with no ; no longer loops forever at the end of the file.

BINDINGS moved from main.rs to lib.rs so the tests can use it. The README lists the new rules and the test command.

Verification

  • cargo test in shaders/msl2wgsl: 48 passed (46 snippet tests, 2 golden).
  • cargo run --features check -- --check: all 7 modules ok.
  • Before and after --out comparison of every module: the only change is the round helper and its 2 call sites in frame.
  • I edited a golden file and confirmed the test fails with the bless command, then restored it.
  • Not run locally: the parity job, which renders every scene with the Metal app and the wgpu build at a 1% pixel tolerance. It covers the merger scenes, and the Mac snapshots don't depend on the translator.

Notes

  • Mac and Windows: shader clock re-bases out of sight, so scenes no longer jump every 13 h 20 min #137 (C1, shader clock) edits shaders/metal. Whichever of the two merges second needs DEEPFIELD_BLESS=1 cargo test --test golden in shaders/msl2wgsl and a commit of the new tests/golden/*.wgsl. After this PR, any .metal change that alters the WGSL needs the same step.
  • .github/workflows/ci.yml: adds one step, cargo test --release --locked, to the shaders job. B3 and T3 also edit this file; the change is a single step, so it should merge without trouble.
  • render-wgpu/build.rs watches all of shaders/, so a change to the golden files also triggers a render-wgpu rebuild (T3 narrows that watch).
  • No user-facing change: no-changelog.

Snippet tests for each rule of the supported Metal subset (validated with naga), golden WGSL for
every module (DEEPFIELD_BLESS=1 cargo test --test golden rewrites it), and errors with file and
line for what was silently accepted: unknown sampler properties or combinations, a mip level on
read/get_width, builtins and struct constructors with the wrong argument count, kernels without
a workgroup size, renames that match nothing. round now takes halves away from zero as in Metal.
Local array-size constants are scoped to their block. CI runs the tests.
@narrowstacks narrowstacks added the no-changelog Needs no CHANGELOG entry label Dec 1, 2026
@narrowstacks
narrowstacks merged commit 322c0c6 into main Dec 1, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog Needs no CHANGELOG entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant